﻿package smooth.core.service.gesture {
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.Event
	import flash.display.InteractiveObject;
	import com.foxaweb.ui.gesture.*;
	
	public class GestureService extends MovieClip {
		
		private var mg:TUIOGesture;
		private var pZone:InteractiveObject;
		private var minFiability:int = 0;
		
		
		public function GestureService(pZone:InteractiveObject) {
			this.pZone = pZone;
			this.mg = new TUIOGesture(pZone);	
			mg.addGesture("A","71");
			mg.addGesture("B","260123401234");
			mg.addGesture("C","43210");
			mg.addGesture("D","26701234",dMatch);
			mg.addGesture("E","4321043210");
			mg.addGesture("F","42");
			mg.addGesture("G","432107650",gMatch);
			mg.addGesture("H","267012");
			mg.addGesture("I","2");
			mg.addGesture("J","234");
			mg.addGesture("K","3456701");
			mg.addGesture("L","20");
			mg.addGesture("M","6172");
			mg.addGesture("N","616");
			mg.addGesture("O","432107654",oMatch);
			mg.addGesture("P","26701234",pMatch);
			mg.addGesture("Q","4321076540",qMatch);
			mg.addGesture("R","267012341");
			mg.addGesture("S","432101234",sMatch);
			mg.addGesture("T","02");
			mg.addGesture("U","21076");
			mg.addGesture("V","17");
			mg.addGesture("W","2716");
			mg.addGesture("X","1076543");
			mg.addGesture("Y","21076234567");
			mg.addGesture("Z","030");
			mg.addGesture("0","4321076542");
			mg.addGesture("1","2");
			mg.addGesture("2","701230");
			mg.addGesture("3","0123401234");
			mg.addGesture("4","302");
			mg.addGesture("5","4201234",fiveMatch);
			mg.addGesture("6","43210765",sixMatch);
			mg.addGesture("7","03");
			mg.addGesture("8","4321234567654");
			mg.addGesture("9","43210762");
			mg.addGesture("BACKSPACE","4");
			mg.addGesture("SPACE","0");
			mg.addGesture("DOT",".");

			this.mg.addEventListener(GestureEvent.GESTURE_MATCH,matchHandler);
			this.mg.addEventListener(GestureEvent.NO_MATCH,noMatchHandler);
			this.mg.addEventListener(GestureEvent.START_CAPTURE,startHandler);
			this.mg.addEventListener(GestureEvent.STOP_CAPTURE,stopHandler);
		}

		protected function matchHandler(e:GestureEvent):void {
			trace(e.datas+"::fiabilité::"+e.fiability);
			if (e.fiability >= minFiability) {
				switch (e.datas){
					case "K":
					case "X":
						if (this.pZone != null) {
							this.mg.setListeningStatus(TUIOGesture.IDLE);
							this.pZone.removeComponent();
						}
					break;
					//case "O":
					case "V":
						if (this.pZone != null) {
							this.pZone.doChangeDisplay();
						}
					break;
					default:
					break;
				}
			}
		}

		/**
		* G - Special case
		*/
		protected function gMatch(infos:Object):uint{
			var py:Number=(infos.lastPoint.y-infos.rect.y)/(infos.rect.height);
			return py>.3 ? infos.cost : 10000;
		}
		
		/**
		* Q - Special case
		*/
		protected function qMatch(infos:Object):uint{
			var py:Number=(infos.lastPoint.y-infos.rect.y)/(infos.rect.height);
			return py<.3 ? infos.cost : 10000;
		}
		
		/**
		* D - Special case
		*/
		protected function dMatch(infos:Object):uint{
			var py:Number=(infos.lastPoint.y-infos.rect.y)/(infos.rect.height);
			return py>.8 ? infos.cost : 10000;
		}
		
		/**
		* P - Special case
		*/
		protected function pMatch(infos:Object):uint{
			var py:Number=(infos.lastPoint.y-infos.rect.y)/(infos.rect.height);
			return py<.7 ? infos.cost : 10000;
		}
		
		/**
		* O - Special case
		*/
		protected function oMatch(infos:Object):uint{
			var py:Number=(infos.lastPoint.y-infos.rect.y)/(infos.rect.height);
			return py<.3 ? infos.cost : 10000;
		}

		/**
		* 6 - Special case
		*/
		protected function sixMatch(infos:Object):uint{
			var py:Number=(infos.lastPoint.y-infos.rect.y)/(infos.rect.height);
			return py>.3 ? infos.cost : 10000;
		}
		
		/**
		* 5 - Special case
		*/
		protected function fiveMatch(infos:Object):uint{
			var pos:int=infos.moves.join("").indexOf("111");
			return pos==-1 ? infos.cost : 10000;
		}
		
		/**
		* S - Special case
		*/
		protected function sMatch(infos:Object):uint{
			var pos:int=infos.moves.join("").indexOf("111");
			return pos>-1 ? infos.cost : 10000;
		}

		
		protected function noMatchHandler(e:GestureEvent):void{
		}

		protected function startHandler(e:GestureEvent):void{
			addEventListener(Event.ENTER_FRAME,capturingHandler);
		}

		protected function stopHandler(e:GestureEvent):void{
			removeEventListener(Event.ENTER_FRAME,capturingHandler);
		}

		protected function capturingHandler(e:Event):void{
		}

		public function startCatchGesture():void {
			this.mg.setListeningStatus(TUIOGesture.LISTENING);
		}
		
		public function endCatchGesture():void {
			this.mg.setListeningStatus(TUIOGesture.IDLE);
			this.mg.endCapture();
		}
		
	}
	
}